﻿using System.Web;
using WorkSummary.Bussiness.Bll;
using WorkSummary.Bussiness.Model;
using WorkSummary.Common;
using WorkSummary.Common.Provider;


namespace WorkSummary.Business
{
    public class SysVisitor
    {
        public static SysVisitor Instance
        {
            get { return SingletonProvider<SysVisitor>.Instance; }
        }

        #region Session Key
        /// <summary>
        /// 记录用户ID
        /// </summary>
        public const string SessionUserIDKey = "WorkSummary-ADMIN-USERID";
        /// <summary>
        /// 记录用户账号
        /// </summary>
        public const string SessionAccountNumKey = "WorkSummary-ADMIN-ACCOUNTNUM";
        /// <summary>
        /// 记录用户昵称
        /// </summary>
        public const string SessionUserNameKey = "WorkSummary-ADMIN-UserName";
        /// <summary>
        /// 记录用户角色
        /// </summary>
        public const string SessionRoleTypeKey = "WorkSummary-ADMIN-ROLETYPE";
        /// <summary>
        /// 记录用户
        /// </summary>
        public const string SessionUser = "WorkSummary-ADMIN";

        #endregion

        #region CookieName Key
        /// <summary>
        /// Cookie的名字
        /// </summary>
        public const string CookieNameKey = "WorkSummary-COOKIE-NAME";
        /// <summary>
        /// 用户账号
        /// </summary>
        public const string CookieAccountNumKey = "WorkSummary-COOKIE-ACCOUNTNUM";
        /// <summary>
        /// 用户昵称
        /// </summary>
        public const string CookieUserNameKey = "WorkSummary-COOKIE-UserName";
        /// <summary>
        /// 用户密码
        /// </summary>
        public const string CookiePasswordKey = "WorkSummary-COOKIE-PASSWORD";
        /// <summary>
        /// 用户角色
        /// </summary>
        public const string CookieRoleTypeKey = "WorkSummary-COOKIE-ROLETYPE";
        /// <summary>
        /// 用于保存一些临时参数
        /// </summary>
        public const string CookiesParms = "WorkSummary-COOKIE-Parms";

        #endregion
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserID
        {
            get { return PublicMethod.GetString(HttpContext.Current.Session[SessionUserIDKey]); }
            set { HttpContext.Current.Session[SessionUserIDKey] = value; }
        }
        /// <summary>
        /// 用户账号
        /// </summary>
        public string AccountNum
        {
            get { return PublicMethod.GetString(HttpContext.Current.Session[SessionAccountNumKey]); }
            set { HttpContext.Current.Session[SessionAccountNumKey] = value; }
        }
        /// <summary>
        /// 用户昵称(用户名)
        /// </summary>
        public string UserName
        {
            get { return PublicMethod.GetString(HttpContext.Current.Session[SessionAccountNumKey]); }
            set { HttpContext.Current.Session[SessionAccountNumKey] = value; }
        }
        /// <summary>
        /// 用户角色名称(用户录入)
        /// </summary>
        //public string RoleType
        //{
        //    get { return PublicMethod.GetString(HttpContext.Current.Session[SessionRoleTypeKey]); }
        //    set { HttpContext.Current.Session[SessionRoleTypeKey] = value; }
        //}
        /// <summary>
        /// 用户对象
        /// </summary>
        public MUser CurrentUser
        {
            get { return (MUser)HttpContext.Current.Session[SessionUser]; }
            set { HttpContext.Current.Session[SessionUser] = value; }
        }
        /// <summary>
        /// 用户权限
        /// </summary>
        //public object Right
        //{
        //    get { return BUser.Instance.GetRight(RoleType); }
        //}
        /// <summary>
        /// 用户登出
        /// </summary>
        public void LoginOut()
        {
            CookieHelper.ClearUserCookie("", SysVisitor.CookieNameKey);
            AccountNum = null;
            UserID = null;
        }
        /// <summary>
        /// 判断该用户是否在登录状态，如果服务器能取到session则尝试重新写cookie
        /// </summary>
        public bool IsGuest
        {
            get
            {
                if (string.IsNullOrEmpty(AccountNum))
                    return !BUser.Instance.UserLogin();
                return false;
            }
        }
    }
}
